<?php
namespace app\admin\controller;

use app\AdminBaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Filesystem;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Cache;


class Index extends AdminBaseController {

    public function index() {
    	$a = Db::table('yhq_tickets')->select();
    	print_r($a);
    }

    public function getGoods() {
        $data = Db::table('yhq_tickets')->order('id desc')->paginate(10);
        return $data;
    }

    public function delGoods($id) {
        $ids = explode(',',$id);
    	$result = Db::table('yhq_tickets')->where('id','in',$ids)->delete();
    	$res;
    	if($result) {
    		$res = array('success' => true );
    	} else {
    		$res = array('success' => false );
    	}
    	return $res;
    }

    public function delOutDateGoods() {
        $currentTime = date("Y-m-d H:m:s",time());
        $result = Db::table('yhq_tickets')->where('ticket_end_time', '<', $currentTime)->delete();
        $res;
        if($result) {
            $res = array('success' => true );
        } else {
            $res = array('success' => false );
        }
        return $res;
    }


    //上传
    public function upload() {

        $file = Request::file('file');
        //验证文件规则
        $result=validate(['file' => ['fileSize:1024000,fileExt:xls']])->check(['file' => $file]);
        if($result){
            //上传到服务器,
            $path = Filesystem::disk('public')->putFile('upload',$file);
            //结果是 $path = upload/20200825\***.jpg
            
            //图片路径，Filesystem::getDiskConfig('public','url')功能是获取public目录下的storage，
            $picCover = Filesystem::getDiskConfig('public','url').'/'.str_replace('\\','/',$path);
            //结果是 $picCover = storage/upload/20200825/***.jpg
            
            //获取图片名称
            $fileName = $file->getOriginalName();
            

            // 创建读操作
            $reader = IOFactory::createReader('Xls');
            $spreadsheet = $reader->load(public_path().$picCover);
            // 获取活动工作簿
            $sheet = $spreadsheet->getActiveSheet();

            // 获取内容的最大列 如 D
            $highest = $sheet->getHighestColumn();

            // 获取内容的最大行 如 4
            $row = $sheet->getHighestRow();

            // 用于存储表格数据
            $data = [];
            for ($i = 2; $i <= $row; $i++) {
                $t1 = $sheet->getCellByColumnAndRow(1, $i)->getValue();
                $t2 = $sheet->getCellByColumnAndRow(2, $i)->getValue();
                $t3 = $sheet->getCellByColumnAndRow(3, $i)->getValue();
                $t4 = $sheet->getCellByColumnAndRow(4, $i)->getValue();
                $t5 = $sheet->getCellByColumnAndRow(5, $i)->getValue();
                $t6 = $sheet->getCellByColumnAndRow(6, $i)->getValue();
                $t7 = $sheet->getCellByColumnAndRow(7, $i)->getValue();
                $t8 = $sheet->getCellByColumnAndRow(8, $i)->getValue();
                $t9 = $sheet->getCellByColumnAndRow(9, $i)->getValue();
                $t10 = $sheet->getCellByColumnAndRow(10, $i)->getValue();
                $t11 = $sheet->getCellByColumnAndRow(11, $i)->getValue();
                $t12 = $sheet->getCellByColumnAndRow(12, $i)->getValue();
                $t13 = $sheet->getCellByColumnAndRow(13, $i)->getValue();
                $t14 = $sheet->getCellByColumnAndRow(14, $i)->getValue();
                $t15 = $sheet->getCellByColumnAndRow(15, $i)->getValue();
                $t16 = $sheet->getCellByColumnAndRow(16, $i)->getValue();
                $t17 = $sheet->getCellByColumnAndRow(17, $i)->getValue();
                $t18 = $sheet->getCellByColumnAndRow(18, $i)->getValue();
                $t19 = $sheet->getCellByColumnAndRow(19, $i)->getValue();
                $t20 = $sheet->getCellByColumnAndRow(20, $i)->getValue();
                $t21 = $sheet->getCellByColumnAndRow(21, $i)->getValue();

                $data[] = [
                    'goods_id'          =>  $t1, 
                    'goods_name'        =>  $t2, 
                    'img_url'           =>  $t3, 
                    'goods_url'         =>  $t4, 
                    'shop_name'         =>  $t5, 
                    'price'             =>  $t6, 
                    'sales_volume'      =>  $t7, 
                    'rate'              =>  number_format($t8,2), 
                    'commission'        =>  $t9, 
                    'wang_name'         =>  $t10, 
                    'promote_short_link'=>  $t11, 
                    'promote_link'      =>  $t12, 
                    'command'           =>  $t13, 
                    'ticket_count'      =>  $t14, 
                    'ticket_last'       =>  $t15, 
                    'ticket_price'      =>  $t16, 
                    'ticket_start_time' =>  $t17, 
                    'ticket_end_time'   =>  $t18, 
                    'ticket_link'       =>  $t19, 
                    'ticket_command'    =>  $t20, 
                    'ticket_short_link' =>  $t21
                ];
            }
            
            foreach ($data as $key => $value) {
                Db::table('yhq_tickets')->insert($value);
            }
            unlink(public_path().$picCover);
            $result = json_encode(array('success'=>true,'message'=>'导入成功'));
            print_r($result);            
        }

    }

    //退出
    public function logout() {
        Cache::clear('uid');
        $res = array('success' => true,'message'=>'退出成功');
        return $res;
    }

    public function updatePassword() {
        $uid = Cache::get('uid');
        $user = Db::table('yhq_user')->where(array('id'=>$uid))->find();
        $result;
        if($user['password']==md5($_POST['oldPass'])) {
            $res = Db::table('yhq_user')->where(array('id'=>$uid))->update(['password' => md5($_POST['pass'])]);
            if(!empty($res)) {
                $result = array('success'=>true,'message'=>'修改成功，请重新登录！');
                Cache::clear('uid');
            } else {
                $result = array('success'=>false,'message'=>'修改失败！');
            }     
        } else {
            $result = array('success'=>false,'message'=>'原密码错误！');
        }
        return $result;
    }
}
